import os
from PIL import Image

MyPath = 'C:/Users/Admin/Desktop/icon2/'
OutPath = 'C:/Users/Admin/Desktop/icon3/'

def processImage(filesoure, destsoure, name, imgtype):

    imgtype = 'bmp' if imgtype == '.bmp' else 'png'
    im = Image.open(filesoure + name)
    img = im.convert("RGBA")
    pixdata = img.load()
    for y in range(img.size[1]):
        for x in range(img.size[0]):
            pixdata[x, y] = (pixdata[x, y][0], pixdata[x, y][2], pixdata[x, y][1], pixdata[x, y][3])

    img.save(destsoure + name, imgtype)

def run():
    os.chdir(MyPath)
    for i in os.listdir(os.getcwd()):
        postfix = os.path.splitext(i)[1]
        if postfix == '.bmp' or postfix == '.png':
            processImage(MyPath, OutPath, i, postfix)

if __name__ == '__main__':
    run()

